﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using WHC.Hotel.BLL;
using WHC.Pager.Entity;

using WHC.Hotel.Entity;
using WHC.OrderWater.Commons;

namespace WHC.Hotel.UI
{
    public partial class FrmBookTeaManager : BaseForm
    {
        public FrmBookTeaManager()
        {
            InitializeComponent();
        }

        private void FrmBookRoomManager_Load(object sender, EventArgs e)
        {
            BindData();

            this.winGridViewPager1.ProgressBar = this.toolStripProgressBar1.ProgressBar;
            this.winGridViewPager1.OnPageChanged += new EventHandler(winGridViewPager1_OnPageChanged);
            this.winGridViewPager1.OnStartExport += new EventHandler(winGridViewPager1_OnStartExport);
            this.winGridViewPager1.OnEditSelected += new EventHandler(winGridViewPager1_OnEditSelected);
            this.winGridViewPager1.OnAddNew += new EventHandler(winGridViewPager1_OnAddNew);
            this.winGridViewPager1.OnRefresh += new EventHandler(winGridViewPager1_OnRefresh);
            this.winGridViewPager1.OnDeleteSelected += new EventHandler(winGridViewPager1_OnDeleteSelected);

            this.winGridViewPager1.AppendedMenu = this.contextMenuStrip1;
        }

        private void winGridViewPager1_OnRefresh(object sender, EventArgs e)
        {
            BindData();
        }

        private void winGridViewPager1_OnDeleteSelected(object sender, EventArgs e)
        {
            if (MessageUtil.ShowYesNoAndTips("您确定删除选定的记录么？") == DialogResult.No)
            {
                return;
            }

            DataGridView grid = this.winGridViewPager1.dataGridView1;
            if (grid != null)
            {
                foreach (DataGridViewRow row in grid.SelectedRows)
                {
                    BLLFactory<TeaBook>.Instance.Delete(row.Cells["ID"].Value.ToString());
                }
                BindData();
            }
        }

        private void winGridViewPager1_OnEditSelected(object sender, EventArgs e)
        {
            DataGridView grid = this.winGridViewPager1.dataGridView1;
            if (grid != null)
            {
                foreach (DataGridViewRow row in grid.SelectedRows)
                {
                    string bookId = row.Cells["ID"].Value.ToString();
                    TeaBookInfo bookInfo = BLLFactory<TeaBook>.Instance.FindByID(bookId);
                    if (bookInfo != null)
                    {
                        FrmBookTeaSuite dlg = new FrmBookTeaSuite();
                        dlg.BookInfo = bookInfo;
                        if (DialogResult.OK == dlg.ShowDialog())
                        {
                            BindData();
                        }
                    }

                    break;
                }
            }
        }

        private void winGridViewPager1_OnAddNew(object sender, EventArgs e)
        {
            FrmBookTeaSuite dlg = new FrmBookTeaSuite();
            dlg.ShowDialog();
            BindData();
        }

        private void winGridViewPager1_OnStartExport(object sender, EventArgs e)
        {
            PagerInfo pagerInfo = new PagerInfo();
            pagerInfo.CurrenetPageIndex = 1;
            pagerInfo.PageSize = int.MaxValue;
            this.winGridViewPager1.AllToExport = BLLFactory<TeaBook>.Instance.GetAllToDataSet(pagerInfo).Tables[0];//product.GetAllToDataSet(pagerInfo).Tables[0];
        }

        private void winGridViewPager1_OnPageChanged(object sender, EventArgs e)
        {
            BindData();
        }

        private void BindData()
        {
            #region 添加别名解析
            this.winGridViewPager1.AddColumnAlias("ID", "编号");
            this.winGridViewPager1.AddColumnAlias("RoomNo", "房间号");
            this.winGridViewPager1.AddColumnAlias("BookNo", "预订单号");
            this.winGridViewPager1.AddColumnAlias("CustomerName", "宾客姓名");
            this.winGridViewPager1.AddColumnAlias("CustomerTel", "联系电话");
            this.winGridViewPager1.AddColumnAlias("CustomerCompany", "公司名称");
            this.winGridViewPager1.AddColumnAlias("Source", "宾客来源");
            this.winGridViewPager1.AddColumnAlias("PreArrivalTime", "预到达时间");
            this.winGridViewPager1.AddColumnAlias("PreOffTime", "预离开时间");
            this.winGridViewPager1.AddColumnAlias("RetentionTime", "保留时间");
            this.winGridViewPager1.AddColumnAlias("Note", "备注");
            this.winGridViewPager1.AddColumnAlias("IsCancel", "是否已经取消");
            #endregion

            SearchCondition condition = new SearchCondition();
            condition.AddCondition("RoomNo", this.txtName.Text, SqlOperator.Like)
                .AddCondition("CustomerName", this.txtName.Text, SqlOperator.Like)
                .AddCondition("CustomerTel", this.txtName.Text, SqlOperator.Like);

            string where = condition.BuildConditionSql(DatabaseType.SqlServer).Replace("Where", "");
            List<TeaBookInfo> list = BLLFactory<TeaBook>.Instance.Find(where, this.winGridViewPager1.PagerInfo);
            this.winGridViewPager1.DataSource = new WHC.Pager.WinControl.SortableBindingList<TeaBookInfo>(list);

            this.winGridViewPager1.dataGridView1.Refresh();
        }

        private void btnSearch_Click(object sender, EventArgs e)
        {
            BindData();
        }

        private void txtName_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                btnSearch_Click(null, null);
            }
        }

        private void menu_Cancel_Click(object sender, EventArgs e)
        {
            DataGridView dg = this.winGridViewPager1.dataGridView1;
            if (dg.SelectedRows.Count > 0)
            {
                foreach (DataGridViewRow row in dg.SelectedRows)
                {
                    if (row.Cells["ID"].Value != null)
                    {
                        int id = Convert.ToInt32(row.Cells["ID"].Value.ToString());
                        bool iscancel = Convert.ToBoolean(row.Cells["IsCancel"].Value);
                        BLLFactory<TeaBook>.Instance.UpdateBookIn(id, !iscancel);
                        BindData();
                    }
                    break;
                }
            }            
        }

        private void menu_Order_Click(object sender, EventArgs e)
        {
            DataGridView dg = this.winGridViewPager1.dataGridView1;
            if (dg.SelectedRows.Count > 0)
            {
                foreach (DataGridViewRow row in dg.SelectedRows)
                {
                    if (row.Cells["ID"].Value != null)
                    {
                        string roomNo = row.Cells["RoomNo"].Value.ToString();
                        TeaSuiteInfo roomInfo = BLLFactory<TeaSuite>.Instance.FindByRoomNo(roomNo);

                        FrmBookTeaCondition dlg = new FrmBookTeaCondition();
                        dlg.RoomInfo = roomInfo;
                        if (dlg.ShowDialog() == DialogResult.OK)
                        {
                            FrmEditTeaOrder dlgOrder = new FrmEditTeaOrder();
                            dlgOrder.RoomInfo = roomInfo;
                            if (dlg.bookInfo == null)
                            {
                                dlgOrder.ShowDialog();
                            }
                            else
                            {
                                TeaBookInfo bookInfo = dlg.bookInfo;
                                if (dlgOrder.ShowDialog() == DialogResult.OK)
                                {
                                    //删除预订
                                    BLLFactory<TeaBook>.Instance.Delete(bookInfo.ID.ToString());
                                }
                            }
                        }
                    }
                    break;
                }
            }          
        }
    }
}
